package com.demo.java.jvm.jol;

import com.demo.java.utils.ThreadUtils;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;

import java.util.concurrent.TimeUnit;

public class JOLTest {
    public static void main(String[] args) {
        Object obj = new Object();


        // -XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
        // 启用偏向锁(默认开启), 并修改偏向锁启动延长时间为0(默认5s)
        // -XX:-UseCompressedOops
        // 关闭指针压缩

        // 查看对象信息
        // 001 无锁
        System.out.println(ClassLayout.parseInstance(obj).toPrintable());
        ThreadUtils.sleep(5, TimeUnit.SECONDS);
        synchronized (obj) {
            System.out.println("test");
        }
        new Thread(() -> {
            synchronized (obj) {
                System.out.println("sub thread");
            }
        }).start();
        // 101 偏向锁
        System.out.println(ClassLayout.parseInstance(obj).toPrintable());

        System.out.println(ClassLayout.parseInstance(new String[]{}).toPrintable());

        /*
        System.out.println(ClassLayout.parseInstance(obj).toPrintable());*/
        // 查看对象外部信息
//        System.out.println(GraphLayout.parseInstance(obj).toPrintable());
        // 查看对象总大小
//        System.out.println(GraphLayout.parseInstance(obj).totalSize());
    }
}
